import Vue from 'vue'
import Router from 'vue-router'
import account from './account'
import buyer from './buyer'
import finance from './finance'
import seller from './seller'
import store from '../store'

Vue.use(Router);

const routes = [
    {
        path: '/login',
        name: `login`,
        meta: {
            title: '登陆',
            isFull: true
        },
        components: {
            default: resolve => require(['../pages/login'], resolve),
        }
    },
    {
        path: '/qqLogin',
        name: `qqLogin`,
        meta: {
            title: '绑定账号',
            isFull: true

        },
        components: {
            default: resolve => require(['../pages/qqLogin'], resolve),
        }
    },
    {
        path: '/search',
        name: `search`,
        meta: {
            title: '搜索',
        },
        components: {
            default: resolve => require(['../pages/search'], resolve),
        }
    },
    {
        path: '/productDetail',
        name: `productDetail`,
        meta: {
            title: '商品详情',
        },
        components: {
            default: resolve => require(['../pages/productDetail'], resolve),
        }
    },

    {
        path: '/promotion',
        name: `promotion`,
        meta: {
            title: '推广返利',

        },
        components: {
            default: resolve => require(['../pages/promotion'], resolve),
        }
    },
    {
        path: '/task',
        name: `task`,
        meta: {
            title: '任务大厅',

        },
        components: {
            default: resolve => require(['../pages/task'], resolve),
        }
    },
    {
        path: '/substation/create',
        name: `create`,
        meta: {
            title: '分站搭建',

        },
        components: {
            default: resolve => require(['../pages/substation/create'], resolve),
        }
    },
    {
        path: '/contactUs',
        name: `contactUs`,
        meta: {
            title: '联系我们',

        },
        components: {
            default: resolve => require(['../pages/contactUs'], resolve),
        }
    },
    ...account,
    ...buyer,
    ...finance,
    ...seller
];

const router = new Router({
    routes
});

//全局路由钩子
router.beforeEach((to, from, next) => {

    let stationTitle = (store.state.subStation && store.state.subStation.fenzhan_mingzi) || '';
    let title;
    if (stationTitle) {
        title = stationTitle + '-' + to.meta.title;
    } else {
        title = to.meta.title;
    }
    document.title = title
    if (!to.meta.hide) store.commit('SET_MENU', to.path);


    //如果是刷新的话 重新获取分站信息
    if (!from.name || to.name === 'login') {
        store.dispatch('getSubStation');
        store.dispatch('getBrands');

    }


    store.commit('SET_DIALOG', !!+localStorage.shanghu_zhuangtai);
    /*if (localStorage.user) {
        if (from.name === 'login' || from.name === 'qqLogin') {
            next('/')
        } else {
            if (to.name === 'login' || to.name === 'qqLogin') {
                next(false);
            } else {
                next();
            }
        }

    } else {
        if (to.name !== 'login') {
            next()
        } else {
            next()
        }

    }*/
    next()


});
export default router